home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmlist.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  4.7 KB  |  105 lines

  1. // CmList.h
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Singly linked list definition.
  7. // -----------------------------------------------------------------
  8.  
  9. #ifndef _CMLIST_H
  10. #define _CMLIST_H
  11.  
  12. #include <cm/include/cmcont.h>
  13.  
  14. class CmLink;                                     // List link class stub.
  15. class CmLinkedListIterator;                       // Iterator class stub.
  16.  
  17. class CmLinkedList : public CmContainer {         // Linked list definition.
  18. public:
  19.   CmLinkedList() : _first(NULL), _last(NULL) {}   // Construct list.
  20.   CmLinkedList(const CmLinkedList&);              // List copy constructor.
  21.  ~CmLinkedList();                                 // List destructor.
  22.  
  23.   CmLinkedList& operator= (const CmLinkedList&);  // Assignment operator.
  24.   CmObject*     operator[](int) const;            // Get object by index.
  25.  
  26.   Bool        add         (CmObject*);            // Add to end of list.
  27.   Bool        append      (CmObject*);            // Add to end of list.
  28.   Bool        prepend     (CmObject*);            // Add to start of list.
  29.   Bool        insertAfter (CmObject*, CmObject*); // Insert after another.
  30.   Bool        insertBefore(CmObject*, CmObject*); // Insert before another.
  31.   Bool        remove      (CmObject*);            // Remove object from list.
  32.   CmObject*   removeFirst ();                     // Remove first object.
  33.   CmObject*   removeLast  ();                     // Remove last object.
  34.   CmObject*   lookup      (CmObject*) const;      // Find equal object.
  35.   CmObject*   first       () const;               // Get first object in list.
  36.   CmObject*   last        () const;               // Get last object in list.
  37.   Bool        contains    (CmObject*) const;      // See if list contains.
  38.   unsigned    occurrences (CmObject*) const;      // Count occurrences of obj.
  39.   void        removeAll   ();                     // Clear the list.
  40.   CmIterator* newIterator () const;               // Create list iterator.
  41.  
  42.   CMOBJECT_DEFINE(CmLinkedList, CmContainer)      // Define object funcs.
  43.  
  44. protected:
  45.   CmLink *_first;                                 // First link in list.
  46.   CmLink *_last;                                  // Last link in list.
  47.   friend  CmLinkedListIterator;                   // Iterator can access.
  48. };
  49.  
  50. class CmLinkedListIterator : public CmIterator {  // Iterator definition.
  51. public:
  52.   CmLinkedListIterator(const CmLinkedList& L)     // Iterator construct.
  53.                       : _list(L), _link(L._first) {}
  54.  
  55.   Bool      done    () const;                     // Check if end.
  56.   CmObject* next    ();                           // Return and advance.
  57.   CmObject* previous();                           // Return and backup.
  58.   CmObject* current () const;                     // Get current object.
  59.   void      first   ();                           // Move to first object.
  60.   void      last    ();                           // Move to last object.
  61.  
  62.   CMOBJECT_DEFINE(CmLinkedListIterator, CmIterator)  // Define object funcs.
  63.  
  64. protected:
  65.   CmLink             *_link;                      // Current list link.
  66.   const CmLinkedList& _list;                      // List being iterated.
  67.   friend              CmLinkedList;               // List class can access.
  68. };
  69.  
  70. class CmLink {                                    // List link class definition.
  71. protected:
  72.   CmLink(CmObject *O) : _next(NULL), _data(O) {}  // Link constructor.
  73.  
  74.   CmLink   *_next;                                // Pointer to next link.
  75.   CmObject *_data;                                // Pointer to object.
  76.   friend    CmLinkedList;                         // List class can access.
  77.   friend    CmLinkedListIterator;                 // Iterator class can access.
  78. };
  79.  
  80. // "first" returns the first object in the list.
  81. inline CmObject* CmLinkedList::first() const
  82. { return (_first) ? _first->_data : NULL; }
  83.  
  84. // "last" returns the last object in the list.
  85. inline CmObject* CmLinkedList::last() const
  86. { return (_last) ? _last->_data : NULL; }
  87.  
  88. // "done" checks if the iterator can advance any further.
  89. inline Bool CmLinkedListIterator::done() const
  90. { return _link == NULL; }
  91.  
  92. // "current" returns the current object in the list.
  93. inline CmObject* CmLinkedListIterator::current() const
  94. { return (_link) ? _link->_data : NULL; }
  95.  
  96. // "first" resets the iterator to the start of the list.
  97. inline void CmLinkedListIterator::first()
  98. { _link = _list._first; }
  99.  
  100. // "last" moves the iterator to the last object.
  101. inline void CmLinkedListIterator::last()
  102. { _link = _list._last; }
  103.  
  104. #endif
  105.